'''
READ_COILS = 0x01                   读线圈寄存器
READ_DISCRETE_INPUTS = 0x02         读离散输入寄存器
READ_HOLDING_REGISTERS = 0x03       读保持寄存器
READ_INPUT_REGISTER = 0x04          读输入寄存器
H05 = 0x05                          写单个线圈寄存器
WRITE_MULTIPLE_COILS = 0x0F         写多个线圈寄存器
WRITE_SINGLE_REGISTER = 0x06        写单个保持寄存器
WRITE_MULTIPLE_REGISTERS = 0x10     写多个保持寄存器
'''

from serial import Serial
import machine
import time

modbus_485 =  Serial(2,baudrate=9600)

# ######################## READ_COILS  #########################
'''
slave_addr          设备地址
starting_address    寄存器开始地址
coil_quantity       读取数据个数  最少8个
'''
def H01(slave_addr,starting_address,coil_quantity):    
    coil_status = modbus_485.read_coils(slave_addr, starting_address, coil_quantity)
    print('Coil status: ' + ' '.join('{:d}'.format(x) for x in coil_status))


# ##################### READ_DISCRETE_INPUTS  ##################
'''
slave_addr          设备地址
starting_address    寄存器开始地址
input_quantity       读取数据个数
'''
def H02(slave_addr, starting_address, input_quantity):
    input_status = modbus_485.read_discrete_inputs(slave_addr, starting_address, input_quantity)
    print('Input status: ' + ' '.join('{:d}'.format(x) for x in input_status))

# ##################### READ HOLDING REGISTERS ##################
'''
slave_addr          设备地址
starting_address    寄存器开始地址
register_quantity   读取数据个数
signed              是否为有符号数 
'''
def H03(slave_addr, starting_address, register_quantity, signed=True):
    register_value = modbus_485.read_holding_registers(slave_addr, starting_address, register_quantity, signed)
    print('Holding register value: ' + ' '.join('{:d}'.format(x) for x in register_value))

# ##################### READ INPUT REGISTERS ##################
'''
slave_addr          设备地址
starting_address    寄存器开始地址
register_quantity   读取数据个数
signed              是否为有符号数 
'''
def H04(slave_addr, starting_address, register_quantity, signed=True):
    register_value = modbus_485.read_input_registers(slave_addr, starting_address, register_quantity, signed)
    print('Input register value: ' + ' '.join('{:d}'.format(x) for x in register_value))


##################### WRITE SINGLE COIL ##################
'''
slave_addr          设备地址
output_address      寄存器地址
output_value        写入数据
'''
def H05(slave_addr, output_address, output_value):
    modbus_485.write_single_coil(slave_addr, output_address, output_value)


##################### WRITE SINGLE REGISTER ##################
'''
slave_addr          设备地址
register_address    寄存器地址
register_value      写入数据
signed              是否为有符号数 
'''
def H06(slave_addr, register_address, register_value, signed=True):
    modbus_485.write_single_register(slave_addr, register_address, register_value, signed)

# ##################### WRITE MULIPLE COILS ##################
'''
slave_addr          设备地址
starting_address    寄存器地址
output_values       写入数据
'''
def H0f(slave_addr, starting_address, output_values):
    modbus_485.write_multiple_coils(slave_addr, starting_address, output_values)


# ##################### WRITE MULIPLE REGISTERS ##################
'''
slave_addr          设备地址
register_address    寄存器地址
register_values     写入数据
signed              是否为有符号数 
'''
def H10(slave_addr, register_address, register_values, signed=True):
    modbus_485.write_multiple_registers(slave_addr, register_address, register_values, signed)




def control_coil():
    H05(2,0,0xff00) #控制单路DO
    time.sleep(1)
    H05(2,1,0xff00)
    time.sleep(1)
    H05(2,2,0xff00)
    time.sleep(1)
    H05(2,3,0xff00)
    time.sleep(1)
    H01(2,0,4)   #读取4个DI状态
    H05(2,0,0)
    time.sleep(1)
    H05(2,1,0)
    time.sleep(1)
    H05(2,2,0)
    time.sleep(1)
    H05(2,3,0)
    time.sleep(1)
    H01(2,0,4) 


#AI 采样范围寄存器，寄存器地址（0x0DAC）写入范围后读取，0 表示 0-20mA， 1 表示 4-20mA
def write_Registers():
    H10(2,0xdac,[1,1,1,1])  #4路同时写入1
    H03(2,0xdac,4)    #读取
    H06(2,0xdac,0)     #第一路改成0
    H03(2,0xdac,4)   #读取


if __name__ == '__main__':
    control_coil()      ##依次控制DO1-DO4 
    H0f(2,0,[1,1,1,1])    #开启全部DO
    time.sleep(1)
    # H0f(2,0,[0,0,0,0])    #关闭全部DO
    H02(2,0,4)            #获取4路DI数值
    H04(2,0,4)            #获取4路AI数值
    write_Registers()     #控制采样寄存器
